﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WLYT.Application.Interfaces;
using WLYT.Domain.Entries;
using WLYT.Domain.Model;

namespace WLYT.WMS.Controllers
{
    [ApiController]
    [Authorize]
    public class SysMenuController(IBaseService<Sys_Menus> sysMenusService, IBaseService<Sys_Operation> sysOperationService) : ControllerBase
    {
        [Route("api/menu/GetList")]
        [HttpGet]
        public async Task<List<Tree>> GetList()
        {
            List<Tree> children = await RouteTreeLoad();
            List<Tree> result = new List<Tree>
            {
                new Tree
                {
                    key = "*",
                    title = "全部",
                    children = children
                }
            };
            return result;
        }

        private async Task<List<Tree>> RouteTreeLoad()
        {
            List<Sys_Menus> menus = await sysMenusService.GetListAsync(true);
            IList<Sys_Operation> operations = await sysOperationService.GetListAsync(true);

            IList<Sys_Menus> parentRouteData = menus.Where(r => r.MenuParentId == 0).OrderBy(m => m.Sort).ToList();

            List<Tree> routeData = new List<Tree>();

            foreach (Tree item in parentRouteData.Select(m => new Tree
            {
                title = m.MenuName,
                key = m.MenuPageUrl,
                id = m.Id,
                children = new List<Tree>()
            }))
            {
                await RouteChildListLoad(item, operations);
                routeData.Add(item);
            }
            return routeData;
        }

        private async Task RouteChildListLoad(Tree sItem, IList<Sys_Operation> operations)
        {
            IList<Sys_Menus> menus = await sysMenusService.GetListAsync(true);
            List<Sys_Menus> childRoutes = menus.Where(a => a.MenuParentId == sItem.id).OrderBy(s => s.Sort).ToList();
            if (childRoutes.Count == 0)
            {
                List<Sys_Operation> childOperations = operations.Where(a => a.MenuUrl == sItem.key).ToList();
                foreach (Sys_Operation item in childOperations)
                {
                    sItem.children.Add(new Tree
                    {
                        title = item.Name,
                        key = $"{item.MenuUrl}_{item.Code}",
                        id = item.Id,
                        children = new List<Tree>(),
                    });
                };
            }
            else
            {
                foreach (Tree item in childRoutes.Select(m => new Tree
                {
                    title = m.MenuName,
                    key = m.MenuPageUrl,
                    id = m.Id,
                    children = new List<Tree>(),
                }))
                {
                    await RouteChildListLoad(item, operations);
                    sItem.children.Add(item);
                }
            }
        }
    }
}
